﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Administration;
using SharePointCommander.Model;
using SharePointCommander.Binding;
using SharePointCommander.Model.TreeView;
using SharePointCommander.Model.PropertySheet;
using System.Resources;
using System.Reflection;

namespace SharePointCommander
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Dictionary<TreeViewItem, int> selectedIndexes = new Dictionary<TreeViewItem, int>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                treeView1.TreeView.ItemsSource = new[] { new SCFarm(null, SPFarm.Local) };
                treeView1.TreeView.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(treeView1_SelectedItemChanged);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (treeView1.TreeView.SelectedItem is SCProperties)
            {
                SCProperties properties = treeView1.TreeView.SelectedItem as SCProperties;
                btnSave.IsEnabled = true;
            }
            else if (treeView1.TreeView.SelectedItem is SCTreeViewItem)
            {
                SCTreeViewItem item = treeView1.TreeView.SelectedItem as SCTreeViewItem;
                btnSave.IsEnabled = item.HasUpdate;
            }
            refreshPropertySheet();
        }

        private void MenuItemAbout_Click(object sender, RoutedEventArgs e)
        {
            ResourceManager rm = new ResourceManager("SharePointCommander.Properties.Resources", Assembly.GetExecutingAssembly());
            MessageBox.Show(rm.GetString("AboutText"));
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            refreshTreeItem();
            refreshPropertySheet();
        }

        private void refreshTreeItem()
        {
            SCTreeViewItem parentItem = null;
            if (treeView1.TreeView.SelectedItem is SCProperties)
            {
                SCProperties properties = treeView1.TreeView.SelectedItem as SCProperties;
                parentItem = properties.ParentNode;
            }
            else if (treeView1.TreeView.SelectedItem is SCTreeViewItem)
            {
                SCTreeViewItem item = treeView1.TreeView.SelectedItem as SCTreeViewItem;
                parentItem = item.ParentNode;
            }

            parentItem.Refresh();
        }

        private void refreshPropertySheet()
        {
            if (treeView1.TreeView.SelectedItem is SCProperties)
            {
                SCProperties properties = treeView1.TreeView.SelectedItem as SCProperties;
                listView1.ListView.ItemsSource = BindingHelper.CreatePropertyItems(properties.Properties);
            }
            else if (treeView1.TreeView.SelectedItem is SCTreeViewItem)
            {
                SCTreeViewItem item = treeView1.TreeView.SelectedItem as SCTreeViewItem;
                listView1.ListView.ItemsSource = item.GetObjectFields();
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            treeView1.TreeView.Focus();
            if (treeView1.TreeView.SelectedItem is SCProperties)
            {
                SCProperties properties = treeView1.TreeView.SelectedItem as SCProperties;
                BindingHelper.SavePropertyItems((IList<PropertyItem>)listView1.ListView.ItemsSource, properties.Properties);
                properties.Update();
            }
            else if (treeView1.TreeView.SelectedItem is SCTreeViewItem)
            {
                SCTreeViewItem item = treeView1.TreeView.SelectedItem as SCTreeViewItem;
                item.SetObjectFields((IList<PropertyItem>)listView1.ListView.ItemsSource);
                item.Update();
                refreshTreeItem();
                refreshPropertySheet();
            }
        }

    }
}
